package com.fire.rabbitmq.queue;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ：Mr.Fire
 * @date ：Created in 2021/4/25 17:06
 * @description：主题模式
 * @modified By：
 * @version: $
 */
@Configuration
public class QueueTopic {

    public static final String TOPIC_EXCHANGE = "topicExchange";

    public static final String DIRECT_REGXA = "nr.topic.#";
    public static final String DIRECT_REGXB = "nr.topic.b";
    public static final String DIRECT_REGXC = "nr.topic.c";

    public static final String QUEUE_TOPIC_A = "topic.A";

    public static final String QUEUE_TOPIC_B = "topic.B";

    public static final String QUEUE_TOPIC_C = "topic.C";

    /**
     * 创建一个topic交换机
     * @return
     */
    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE);
    }



    /**
     * 创建队列
     * @return
     */
    @Bean
    Queue queueTopicNameA() {
        return new Queue(QUEUE_TOPIC_A);
    }


    @Bean
    Queue queueTopicNameB() {
        return new Queue(QUEUE_TOPIC_B);
    }

    @Bean
    Queue queueTopicNameC() {
        return new Queue(QUEUE_TOPIC_C);
    }


    /**
     * 将direct.A队列绑定到topicExchange交换机中，使用nr.#作为路由规则
     * @param queueTopicNameA
     * @param topicExchange
     * @return
     */
    @Bean
    Binding bindingExchangeMessageTopicA(Queue queueTopicNameA, TopicExchange topicExchange) {
        return BindingBuilder.bind(queueTopicNameA).to(topicExchange).with(DIRECT_REGXA);
    }

    /**
     * 将direct.B队列绑定到topicExchange交换机中，使用nr.b.key作为路由规则
     * @param queueTopicNameB
     * @param topicExchange
     * @return
     */
    @Bean
    Binding bindingExchangeMessageTopicB(Queue queueTopicNameB, TopicExchange topicExchange) {
        return BindingBuilder.bind(queueTopicNameB).to(topicExchange).with(DIRECT_REGXB);
    }

    /**
     * 将direct.B队列绑定到topicExchange交换机中，使用nr.b.key作为路由规则
     * @param queueTopicNameC
     * @param topicExchange
     * @return
     */
    @Bean
    Binding bindingExchangeMessageTopicC(Queue queueTopicNameC, TopicExchange topicExchange) {
        return BindingBuilder.bind(queueTopicNameC).to(topicExchange).with(DIRECT_REGXC);
    }
}
